//
//  TripListViewController.m
//  RoadLine
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 RedFireNoodles. All rights reserved.
//
//KennethWang
//途行精选
#import "TripListViewController.h"
#import "UIImageView+WebCache.h"
#import "TripListTblCell.h"
#import "AFHTTPRequestOperationManager.h"
#import "Constants.h"
#import "DestinationViewController.h"
#import "HighQualityTripModel.h"
#import "TripDetailViewController.h"
#import "MJRefresh.h"
#import "TripViewController.h"

@interface TripListViewController ()

@property(nonatomic, retain)NSMutableArray *array;
@property(nonatomic, retain)NSMutableArray *modelArray;
@property(nonatomic, retain)UITableView *tableView;
//@property(nonatomic, assign)NSInteger *pageNum;

@end

@implementation TripListViewController
//自定义初始化方法:
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = kHighQualityTripTitle;
        self.modelArray = [NSMutableArray array];
    }
    return self;
}
// 加载刷新:
// - (void)refreshLoading
//{
//    [self.tableView addLegendHeaderWithRefreshingBlock:^{
//        [self.tableView.header endRefreshing];
//    }];
//    [self.tableView addLegendFooterWithRefreshingBlock:^{
//        [self reloadNewData];
//        [self.tableView reloadData ];
//        [self.tableView.footer endRefreshing];
//    }];
//}


- (void)reloadNewData
{
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    dispatch_async(mainQueue, ^{
        //AFN解析数据:
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSString *strUrlTrip = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/trips/", self.type, self.tripID];
        [manager GET:strUrlTrip parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            //解析过程
            //最外层字典
            NSMutableDictionary *dic = responseObject;
            //内层数组:
            self.array = [dic objectForKey:@"items"];
            for (NSInteger i = 0; i < self.array.count; i++) {
                HighQualityTripModel *highQualityTripModel = [[HighQualityTripModel alloc]init];
                [highQualityTripModel setValuesForKeysWithDictionary:self.array[i]];
                [self.modelArray addObject:highQualityTripModel];
                [highQualityTripModel release];
            }
            [self.tableView reloadData];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
        }];
    });
//    self.pageNum++;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
    
    // 改变uinavigationBar 的title颜色
    UILabel *titleLabelPrice = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    titleLabelPrice.font = [UIFont boldSystemFontOfSize:18];
    titleLabelPrice.textColor = [UIColor whiteColor];
    titleLabelPrice.backgroundColor = [UIColor clearColor];
    titleLabelPrice.text = @"    途行精选";
    self.navigationItem.titleView = [titleLabelPrice autorelease];
   
    
    // 建tableView
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.tableView.separatorColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    //调用AFN解析数据的方法;
    [self reloadNewData];
    //返回键Home:
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:kHighQualityTripImageName] style:0 target:self action:@selector(homeBackClick:)];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];
    
    //加载刷新方法调用:
    //     self.pageNum = 1;
    //    [self reloadNewData];
    //    [self refreshLoading];
    
}

//cell点击方法实现
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TripViewController *tripViewController = [[TripViewController alloc]init];
    HighQualityTripModel *hqtm = self.modelArray[indexPath.row];
    //属性传值第二步 赋值;
    tripViewController.highQualityTripModelId = [NSString stringWithFormat:@"%@", [hqtm highQualityTripModelId]];
    tripViewController.htmlLatsStr = @"trackpoints";
    
    [self.navigationController pushViewController:tripViewController animated:YES];
    [tripViewController release];
}

//Home返回键方法实现
- (void)homeBackClick:(UIBarButtonItem *)barButton
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

//必须实现的两个协议方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //重用池:
    static NSString *reuseHightQuality = @"reuseHightQuality";
    TripListTblCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseHightQuality];
    if (cell  == nil) {
        cell = [[[TripListTblCell alloc]initWithStyle:0 reuseIdentifier:reuseHightQuality] autorelease];
    }
    if ([Singleton shareSingleton].isNight) {
        [[Singleton shareSingleton] loadNightModeInfoFormDisk];
    }
    HighQualityTripModel *highQualityTripModel = self.modelArray[indexPath.row];
    cell.highQualityNameLabel.text = highQualityTripModel.name;
    NSURL *url = [NSURL URLWithString:highQualityTripModel.cover_image_w640];
    [cell.highQualityImageView sd_setImageWithURL:url];
    cell.highQualityFoorPrintCountLabel.text = [NSString stringWithFormat:@"%ld足迹    %ld喜欢 ",highQualityTripModel.waypoints, highQualityTripModel.recommendations];
    //占位图代码;
    NSURL *urlPlaceHolder = [NSURL URLWithString:highQualityTripModel.cover_image_w640];
    [cell.highQualityImageView sd_setImageWithURL:urlPlaceHolder placeholderImage:[UIImage imageNamed:@"BEIJING.png"]];
    cell.contentView.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
//    cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backgroundimage"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

//可选择的方法实现:
//单个CEll的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 170;
}
- (void)setLightDayModel {
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:246/255.0 green:85/255.0 blue:91/255.0 alpha:1];
}

- (void)setNightDayModel {
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:4/255.0 green:165/255.0 blue:171/255.0 alpha:1];
}
//内存管理
- (void)dealloc
{
    [_modelArray release];
    [_array release];
    [_tableView release];
    [super dealloc];
}























- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
